//
//  WLLVisitorView.swift
//  WLLWeibo
//
//  Created by 王乐乐 on 16/8/9.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
class WLLVisitorView: UIView {
    /// 声明闭包
    var closure :(() -> ())?
    
    
   override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
     //MARK: - 供外界调用的方法
    func setupVisitorViewInfo(imageName:String?,message:String?)  {
        if let I = imageName, M = message {//代表当前是消息,发现,我,
            iconImageView.image = UIImage(named: I)
            messageLabel.text = M
            circleImageView.hidden = true
        }else{//首页
            setuoAnimation()
        }
    }
    /**
     圆圈动画
     */
    func setuoAnimation()  {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = M_PI*2
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        
        animation.removedOnCompletion = false
        
        circleImageView.layer.addAnimation(animation, forKey: nil)
        
    }
    
    
     //MARK: - 设置视图
    func setupUI () {
//        backgroundColor = RandomColor()
        backgroundColor = RGBColor(237, green: 237, blue: 237)
        //添加控件...注意添加顺序
        addSubview(circleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
       // 添加约束
        circleImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        messageLabel.snp_makeConstraints { (make) in
            //最大行宽已经设置了,也就是已有宽度
            make.top.equalTo(iconImageView.snp_bottom).offset(16)
            make.centerX.equalTo(self)
            make.height.equalTo(36)
        }
        registerButton.snp_makeConstraints { (make) in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        loginButton.snp_makeConstraints { (make) in
            //这里写成. top.size 就报红警
            make.size.top.equalTo(registerButton)
             make.right.equalTo(messageLabel)
        }
        
    }
    
     //MARK: - 懒加载控件
     /// 圆圈
    lazy var circleImageView :UIImageView = UIImageView(imageName:"visitordiscover_feed_image_smallicon")
     /// 遮挡
    lazy var maskImageView:UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    /// 房子
    lazy var iconImageView : UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    ///文字
    lazy var messageLabel:UILabel = {
        let label = UILabel()
        label.textColor = ThemeColor
        label.font = UIFont.systemFontOfSize(15)
        //行宽
        label.preferredMaxLayoutWidth = 240
        label.numberOfLines = 0
        label.textAlignment = .Center
        label.text =  "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        return label
    }()
        /// 注册按钮
    lazy var registerButton :UIButton = {
        let btn = UIButton()
        //切边角让其呈现矩形边框
        var imageO = UIImage(named: "common_button_white_disable")
        imageO = imageO?.stretchableImageWithLeftCapWidth(Int(imageO!.size.width*0.5), topCapHeight:Int(imageO!.size.width*0.5))
//        imageO = [imageO stretchableImageWithLeftCapWidth:imageO.size.width*0.5 topCapHeight:imageO.size.height*0.5];
        btn.setBackgroundImage(imageO, forState: UIControlState.Normal)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        btn.addTarget(self , action: "btnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    /// 登录按钮
    lazy var loginButton :UIButton = {
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        btn.addTarget(self , action: #selector(WLLVisitorView.btnClick), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    /**
     @objc 按照 oc 的方式编译,运行循环 ...而 swift 是编译时...
     */
    /**
     按钮的点击事件是由运行循环监听并且以消息机制传递的
     而Swift为了追求性能(静态类型的语言) 决定了其在编译时期就已经决定好了某个事件应该如何去调用
     而使用了private修饰的方法在运行的时候对于运行循环式不可见的所以会提示找不到xxx方法
     可以使用@objc修饰私有方法,其目的是在于告诉系统此方法使用Objective-C的基于运行时的机制(KVC动态派发消息)来执行方法
     */
   @objc private func btnClick()  {
//        print("\(self)登录注册")
    //执行闭包
    closure!()
    
    }
    
}
